package sdau.hjk.util;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 *@version 1.0
 *@author: huangjiakai
 *@create:2021−03-25 14:56
 *@className:sdau.hjk.util.HttpUtils
 *@description: TODO
 */
public class HttpUtils {
    public static String getHtml(String url){
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = null;
        String html = "";
        CloseableHttpClient httpClient = HttpClients.createDefault();
        try {
            response = httpClient.execute(httpGet);
            if(response.getStatusLine().getStatusCode() == 200){//200表示响应成功
                html = EntityUtils.toString(response.getEntity(),"UTF-8");
            }
            httpClient.close();
            response.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return html;
    }
}